<?php
namespace app\admin\controller;
class Price extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function priceList() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 1 && $this->role != 2) {
            return errorResult();
        }
        
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        $offset = ($page - 1) * $pageSize;
        $where = [
            ['role', '=', $this->role],
            ['owner_id', '=', $this->userid]
        ];
        
        $priceName = isset($params['price_name']) ? trim($params['price_name']) : '';
        if($priceName !== ''){
            $where[] = ['price_name', 'like', '%' . $priceName . '%'];
        }
        
        $repData = [];
        $model = model('ChargePrice');
        
        $repData['list'] = $model
            ->where($where)
            ->field(['price_id', 'price_name', 'charge_type', 'charge_unit', 'critical_power', 'check_time', 'prepayment', 'options', 'class_rate'])
            ->order('price_id desc')
            ->limit($offset, $pageSize)
            ->select();
        
        if(isset($params['init']) && $params['init'] == 1){
            $repData['total'] = $model->where($where)->count();
        }
        
        return successResult($repData);
        
    }
    
    public function save() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 1 && $this->role != 2) {
            return errorResult();
        }
        
        $params = request()->post();

        try {
            $data['price_name'] = isset($params['price_name']) ? trim($params['price_name']) : '';
            if($data['price_name'] === ''){
                return errorResult('请填写价格名称');
            }
            
            $data['charge_type'] = isset($params['charge_type']) ? intval($params['charge_type']) : 1;
            if(!in_array($data['charge_type'], [1, 2])) {
                return errorResult();
            }
            
            $data['charge_unit'] = isset($params['charge_unit']) ? floatval($params['charge_unit']) : -1;
            if($data['charge_unit'] < 0){
                return errorResult('请填写最小计费单位');
            }
            
            $data['max_power'] = isset($params['max_power']) ? intval($params['max_power']) : -1;
            if($data['max_power'] < 0){
                return errorResult('请填写最大功率');
            }
            
            $data['critical_power'] = isset($params['critical_power']) ? intval($params['critical_power']) : -1;
            if($data['critical_power'] < 0){
                return errorResult('请填写充满临界功率');
            }
            
            $data['check_time'] = isset($params['check_time']) ? intval($params['check_time']) : -1;
            if($data['check_time'] < 0){
                return errorResult('请填写延迟检测时间');
            }
            
            $data['prepayment'] = isset($params['prepayment']) ? floatval($params['prepayment']) : 0;
            if($data['prepayment'] <= 0){
                return errorResult('请填写预付费');
            }
            
            $classRate = isset($params['class_rate']) ? $params['class_rate'] : [];
            
            if(count($classRate) != 4){
                return errorResult('请按要求填写完整费率');
            }
            
            $data['class_rate'] = [];
            
            foreach ($classRate as $rate) {
                if($rate['w'] === '' || $rate['q'] === '') {
                    return errorResult('请填写完整数据');
                }

                array_push($data['class_rate'], ['q' => floatval($rate['q']), 'w' => floatval($rate['w'])]);
            }
            
            $options = isset($params['options']) ? $params['options'] : [];
            if(!is_array($options)) {
                return errorResult();
            }
            
            $data['options'] = [];
            
            foreach ($options as $option) {
                if($option['a'] === '' || $option['q'] === '') {
                    return errorResult('请填写完整数据');
                }
                
                array_push($data['options'], ['a' => floatval($option['a']), 'q' => floatval($option['q'])]);
            }

            $priceId = isset($params['price_id']) ? intval($params['price_id']) : 0;
            
            $model = model('ChargePrice');
            
            if($priceId > 0){ //编辑
                $field = array_keys($data);
                array_push($field, 'owner_id', 'role');
                $price = $model->findOne(['price_id' => $priceId], $field);
                if(empty($price) || $price['role'] != $this->role || $price['owner_id'] != $this->userid) {
                    return errorResult();
                }
                
                $updateData = [];
                foreach ($data as $k => $val) {
                    if($price[$k] != $val) {
                        $updateData[$k] = $val;
                    }
                }
                
                if(empty($updateData)) {
                    return successResult('编辑成功');
                }
                
                if(isset($updateData['charge_type'])) {
                    return errorResult();
                }
                
                $rs = $model->modify(['price_id' => $priceId], $updateData);
                if(!$rs) {
                    return errorResult('编辑失败');
                }
                
                return successResult('编辑成功');
                
            }else{ //新增
                $data['owner_id'] = $this->userid;
                $data['role'] = $this->role;
                $rs = $model->add($data);
                if(!$rs) {
                    return errorResult('增加失败');
                }
                
                return successResult('增加成功');
            }
            
        } catch (\Exception $e) {
            return errorResult($e->getMessage());
        }
    }
    
    public function getEditInfo() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 1 && $this->role != 2) {
            return errorResult();
        }
        
        $priceId = request()->get('id', 0, 'intval');
        
        $price = model('ChargePrice')->findOne(['price_id' => $priceId], ['role', 'owner_id', 'price_name', 'charge_type', 'charge_unit', 'max_power', 'critical_power', 'check_time', 'prepayment', 'options', 'class_rate']);
        if(empty($price) || $price['role'] != $this->role || $price['owner_id'] != $this->userid) {
            return errorResult();
        }
        
        return successResult($price);
    }
    
    public function dropSelect() {
        $list = model('ChargePrice')->findAll(['role' => $this->role, 'owner_id' => $this->userid], ['price_id', 'price_name']);
        return successResult($list);
    }
    
}













